package com.itfreer.bpm.api;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import com.itfreer.bpm.flow.api.IBaseWorkFlowService;
import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.flow.findwork.para.FindWorkInfo;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.messager.BpmMessage;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.form.api.BaseRestService;
import com.itfreer.form.api.BaseService;
import com.itfreer.utils.json.JsonUtils;
/**
 * 定义需要使用工作流的项目的rest服务基础类型
 * @author gj
 *
 * @param <T>
 */
public abstract class BpmBaseRestService<T extends IBpmProjectEntity> 
extends BaseRestService<T>  {
	public abstract IBaseWorkFlowService getWorkFlowService(); 
	
	@SuppressWarnings({ "rawtypes" })
	protected Class getJTClass() {
		Class entityClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		return entityClass;
	}
	
	@Override
	protected BaseService<T> getService() {
		// TODO Auto-generated method stub
		return getWorkFlowService();
	}
	/**
	 * 项目查找操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/findWork")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public FindWorkInfo<T> findWork(@Context HttpServletRequest request,String data) throws IOException{
		String bpmKey=null;
		String id=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("bpmKey") && args.get("bpmKey") != null)
				bpmKey = args.get("bpmKey").toString();
			
			if (args.containsKey("id") && args.get("id") != null)
				id = args.get("id").toString();
			
			if (args.containsKey("user") && args.get("user") != null)
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().findWork(bpmKey, id, user);
	}
	
	
	
	/**
	 * 获取下一个环节操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getNextTask")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public OptionInfo getNextTask(@Context HttpServletRequest request,String data) throws IOException{
		String bpmKey=null;
		UserInfo user=null;
		T entity=null;
		/*String sexeid=null;*/
		HashMap<String, Object> args = getRestParam(data);
		
		List<BpmHistoryEntity>editHistory =null;
		if(args!=null) {
			if (args.containsKey("bpmKey") && args.get("bpmKey") != null)
				bpmKey = args.get("bpmKey").toString();

			if (args.containsKey("user") && args.get("user") != null)
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
			
			if (args.containsKey("editHistory") && args.get("editHistory") != null) {
				editHistory = (List<BpmHistoryEntity>) JsonUtils.toBean(args.get("editHistory").toString()
						,JsonUtils.getCollectionType(List.class, BpmHistoryEntity.class));
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().getNextTask(bpmKey, entity,editHistory, user);
	}
	
	/**
	 * 项目提交操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/signTask")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage signTask(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("opttj") && args.get("opttj") != null) {
				oInfo =JsonUtils.toBean(args.get("opttj").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().signTask(oInfo, entity, user);
	}
	
	/**
	 * 获取退回节点
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getBackTask")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public OptionInfo getBackTask(@Context HttpServletRequest request,String data) throws IOException{
		String bpmKey=null;
		UserInfo user=null;
		T entity=null;
		HashMap<String, Object> args = getRestParam(data);
		
		List<BpmHistoryEntity>editHistory =null;
		if(args!=null) {
			if (args.containsKey("bpmKey") && args.get("bpmKey") != null)
				bpmKey = args.get("bpmKey").toString();

			if (args.containsKey("user") && args.get("user") != null)
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
			
			if (args.containsKey("editHistory") && args.get("editHistory") != null) {
				editHistory = (List<BpmHistoryEntity>) JsonUtils.toBean(args.get("editHistory").toString()
						,JsonUtils.getCollectionType(List.class, BpmHistoryEntity.class));
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().getBackTask(bpmKey, entity,editHistory, user);
	}
	
	/**
	 * 项目提交操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/sendBack")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage sendBack(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("optback") && args.get("optback") != null) {
				oInfo =JsonUtils.toBean(args.get("optback").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().sendBack(oInfo, entity, user);
	}
	
	/**
	 * 获取退回节点
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getSkipTask")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public OptionInfo getSkipTask(@Context HttpServletRequest request,String data) throws IOException{
		String bpmKey=null;
		UserInfo user=null;
		T entity=null;
		HashMap<String, Object> args = getRestParam(data);
		
		List<BpmHistoryEntity>editHistory =null;
		if(args!=null) {
			if (args.containsKey("bpmKey") && args.get("bpmKey") != null)
				bpmKey = args.get("bpmKey").toString();

			if (args.containsKey("user") && args.get("user") != null)
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
			
			if (args.containsKey("editHistory") && args.get("editHistory") != null) {
				editHistory = (List<BpmHistoryEntity>) JsonUtils.toBean(args.get("editHistory").toString()
						,JsonUtils.getCollectionType(List.class, BpmHistoryEntity.class));
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().getSkipTask(bpmKey, entity,editHistory, user);
	}
	
	/**
	 * 项目跳转操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/skipTask")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage skipTask(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("optskip") && args.get("optskip") != null) {
				oInfo =JsonUtils.toBean(args.get("optskip").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().skipTask(oInfo, entity, user);
	}
	
	/**
	 * 项目退件操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/disposal")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage disposal(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("optdisposal") && args.get("optdisposal") != null) {
				oInfo =JsonUtils.toBean(args.get("optdisposal").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().disposal(oInfo, entity, user);
	}
	
	/**
	 * 项目退件操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/userDispose")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage userDispose(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("optuserdisposal") && args.get("optuserdisposal") != null) {
				oInfo =JsonUtils.toBean(args.get("optuserdisposal").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().userDispose(oInfo, entity, user);
	}
	

	/**
	 * 项目跳转操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/easter")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage easter(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("opteaster") && args.get("opteaster") != null) {
				oInfo =JsonUtils.toBean(args.get("opteaster").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().easter(oInfo, entity, user);
	}
	
	@Autowired(required=false)
	IRestProofReadUser restProofReadUser;

	
	/**
	 * 基于项目的用户验证功能
	 * @param request 请求
	 * @param requestData 前端请求的数据
	 * @param user 用户
	 */
	public void proofReadUser(HttpServletRequest request
			,String requestData,
			UserInfo user) {
		if(restProofReadUser!=null) {
			this.restProofReadUser.ProofReadUser(request
			,requestData,
			user);
		}
	}
	
	/**
	 * 项目退件操作
	 * @param data
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/processSave")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public BpmMessage processSave(@Context HttpServletRequest request,String data) throws IOException{
		
		OptionInfo oInfo=null;
		T entity=null;
		UserInfo user=null;
		HashMap<String, Object> args = getRestParam(data);
		if(args!=null) {
			if (args.containsKey("optsave") && args.get("optsave") != null) {
				oInfo =JsonUtils.toBean(args.get("optsave").toString(),OptionInfo.class);
			}
		
			if (args.containsKey("user") && args.get("user") != null) {
				user=JsonUtils.toBean(args.get("user").toString(), UserInfo.class);
			}
			
			if (args.containsKey("entity") && args.get("entity") != null) {
				String strEntity=args.get("entity").toString();
				entity = (T) JsonUtils.toBean(strEntity,this.getJTClass());
			}
		}
		this.proofReadUser(request,data,user);
		return getWorkFlowService().processSave(oInfo, entity, user);
	}
}
